var catregory = 0;
var pageNumber = 1;
$(function() {
	showThesis();
	$("#zhuzuo-tables").hide();
	$("#xiangmu-tables").hide();
	$("#zhuanli-tables").hide();
});

/**
 * 设置值
 * 
 * @param queryStr
 *            检索字段
 * @param type
 *            类型
 * @param lableId
 *            标签
 * @param pageNum
 *            页码
 */
function valueSet(queryStr, type, lableId, pageNum) {
	$("#queryStr").val(queryStr);
	$(".type").val(type);
	$("#lableId").val(lableId);
	$("#pageNo").val(pageNum);
}
function pageChange(pageNum) {
	pageNumber = pageNum;
	if (catregory == 1) {
		valueSet("", 1, "", pageNum);
		showPatent();
		$("#zhuzuo-tables").hide();
		$("#xiangmu-tables").hide();
		$("#lunwen-tables").hide();
		$("#zhuanli-tables").show();
	} else if (catregory == 3) {
		valueSet("", 3, "", pageNum);
		showWork();
		$("#zhuanli-tables").hide();
		$("#xiangmu-tables").hide();
		$("#lunwen-tables").hide();
		$("#zhuzuo-tables").show();
	} else if (catregory == 0) {
		valueSet("", 0, "", pageNum);
		showThesis();
		$("#zhuzuo-tables").hide();
		$("#xiangmu-tables").hide();
		$("#zhuanli-tables").hide();
		$("#lunwen-tables").show();
	} else if (catregory == 2) {
		valueSet("", 2, "", pageNum);
		showProject();
		$("#zhuzuo-tables").hide();
		$("#zhuanli-tables").hide();
		$("#lunwen-tables").hide();
		$("#xiangmu-tables").show();
	}
}
function modelChange(value) {
	catregory = value;
	pageNumber = 1;
	if (catregory.trim() == "1") {
		valueSet("", 1, "", 1);
		showPatent();
		$("#zhuzuo-tables").hide();
		$("#xiangmu-tables").hide();
		$("#lunwen-tables").hide();
		$("#zhuanli-tables").show();
	} else if (catregory.trim() == "3") {
		valueSet("", 3, "", 1);
		showWork();
		$("#zhuanli-tables").hide();
		$("#xiangmu-tables").hide();
		$("#lunwen-tables").hide();
		$("#zhuzuo-tables").show();
	} else if (catregory.trim() == "0") {
		valueSet("", 0, "", 1);
		showThesis();
		$("#zhuzuo-tables").hide();
		$("#xiangmu-tables").hide();
		$("#zhuanli-tables").hide();
		$("#lunwen-tables").show();
	} else if (catregory.trim() == "2") {
		valueSet("", 2, "", 1);
		showProject();
		$("#zhuzuo-tables").hide();
		$("#zhuanli-tables").hide();
		$("#lunwen-tables").hide();
		$("#xiangmu-tables").show();
	}
}

/**
 * 论文
 */
function showThesis() {
	var content = "";
	$.post("getArchivesList", $("#queryForm").serialize(), function(
			responseResult) {
		// alert(responseResult.responseParam.totalNum);
		if (responseResult.archivesList.length > 0) {
			$(responseResult.archivesList).each(
					function(i) {
						var statu = "";
						if (this.auditstatus == 0) {
							statu = "未审核";
						} else if (this.auditstatus == 1) {
							statu = "请求审核";
						} else if (this.auditstatus == 2) {
							statu = "审核失败";
						} else if (this.auditstatus == 3) {
							statu = "审核通过";
						}
						content += "<tr><td><input type='checkbox' id='"
								+ this.id + "' class='arch'/>" + "</td><td>"
								+ (i + 1) + "</td><td>" + this.title
								+ "</td><td>" + this.firstauthor + "</td><td>"
								+ this.department + "</td><td>" + statu
								+ "</a></td></tr>";
					});
			$('#thesis').html(content);
			pageShow(pageNumber, responseResult.responseParam.totalNum, '');
		} else {
			$('#thesis').html("无相关数据！");
		}
	});
}

/**
 * 专利
 */
function showPatent() {
	var content = "";
	$.post("getArchivesList", $("#queryForm").serialize(), function(
			responseResult) {
		if (responseResult.archivesList.length > 0) {
			$(responseResult.archivesList).each(
					function(i) {
						var statu = "";
						if (this.auditstatus == 0) {
							statu = "未审核";
						} else if (this.auditstatus == 1) {
							statu = "请求审核";
						} else if (this.auditstatus == 2) {
							statu = "审核失败";
						} else if (this.auditstatus == 3) {
							statu = "审核通过";
						}
						content += "<tr><td><input type='checkbox' id='"
								+ this.id + "' class='arch' />" + "</td><td>"
								+ (i + 1) + "</td><td>" + this.title
								+ "</td><td>" + this.firstauthor + "</td><td>"
								+ this.department + "</td><td>" + statu
								+ "</a></td></tr>";
					});
			$('#patent').html(content);
			pageShow(pageNumber, responseResult.responseParam.totalNum, '');
		} else {
			$('#patent').html("无相关数据！");
		}
	});
}
/**
 * 项目
 */
function showProject() {
	var content = "";
	$.post("getArchivesList", $("#queryForm").serialize(), function(
			responseResult) {
		if (responseResult.archivesList.length > 0) {
			$(responseResult.archivesList).each(
					function(i) {
						var statu = "";
						if (this.auditstatus == 0) {
							statu = "未审核";
						} else if (this.auditstatus == 1) {
							statu = "请求审核";
						} else if (this.auditstatus == 2) {
							statu = "审核失败";
						} else if (this.auditstatus == 3) {
							statu = "审核通过";
						}
						content += "<tr><td><input type='checkbox' id='"
								+ this.id + "' class='arch' />" + "</td><td>"
								+ (i + 1) + "</td><td>" + this.title
								+ "</td><td>" + this.firstauthor + "</td><td>"
								+ this.department + "</td><td>" + statu
								+ "</a></td></tr>";
					});
			$('#project').html(content);
			pageShow(pageNumber, responseResult.responseParam.totalNum, '');
		} else {
			$('#project').html("无相关数据！");
		}
	});
}

/**
 * 著作
 */
function showWork() {
	var content = "";
	$.post("getArchivesList", $("#queryForm").serialize(), function(
			responseResult) {
		if (responseResult.archivesList.length > 0) {
			$(responseResult.archivesList).each(
					function(i) {
						var statu = "";
						if (this.auditstatus == 0) {
							statu = "未审核";
						} else if (this.auditstatus == 1) {
							statu = "请求审核";
						} else if (this.auditstatus == 2) {
							statu = "审核失败";
						} else if (this.auditstatus == 3) {
							statu = "审核通过";
						}
						content += "<tr><td><input type='checkbox' id='"
								+ this.id + "' class='arch' />" + "</td><td>"
								+ (i + 1) + "</td><td>" + this.title
								+ "</td><td>" + this.firstauthor + "</td><td>"
								+ this.department + "</td><td>" + statu
								+ "</a></td></tr>";
					});
			$('#work').html(content);
			pageShow(pageNumber, responseResult.responseParam.totalNum, '');
		} else {
			$('#work').html("无相关数据！");
		}
	});
}

function pageShow(curPage, totalPage, pageFlag) {
	var ta = "";
	var lastPage = curPage - 1, nextPage = curPage + 1;
	if (lastPage < 1) {
		lastPage = 1;
	}
	if (nextPage > totalPage) {
		nextPage = totalPage;
	}
	ta += "<li><a href='javascript:pageChange(" + lastPage
			+ ")'>&laquo;</a></li>";
	if (curPage > 2 && curPage < parseInt(parseInt(totalPage) - 2)) {
		for (var j = parseInt(parseInt(curPage) - 2); j <= totalPage
				&& j <= parseInt(parseInt(curPage) + 2); j++) {
			if (j == curPage) {
				ta += "<li class='am-active'><a href='javascript:;'>" + j
						+ "</a></li>";
			} else {
				ta += "<li><a href='javascript:pageChange(" + j + ");'>" + j
						+ "</a></li>";
			}
		}
	} else if (curPage <= 2 && totalPage >= 5) {
		for (var j = 1; j <= 5; j++) {
			if (j == curPage) {
				ta += "<li class='am-active'><a href='javascript:;'>" + j
						+ "</a></li>";
			} else {
				ta += "<li><a href='javascript:pageChange(" + j + ");'>" + j
						+ "</a></li>";
			}
		}
	} else if (totalPage < 5) {
		for (var j = 1; j <= totalPage; j++) {
			if (j == curPage) {
				ta += "<li class='am-active'><a href='javascript:;'>" + j
						+ "</a></li>";
			} else {
				ta += "<li><a href='javascript:pageChange(" + j + ");'>" + j
						+ "</a></li>";
			}
		}
	} else {
		for (var j = parseInt(parseInt(totalPage) - 4); j <= totalPage; j++) {
			if (j == curPage) {
				ta += "<li class='am-active'><a href='javascript:;'>" + j
						+ "</a></li>";
			} else {
				ta += "<li><a href='javascript:pageChange(" + j + ");'>" + j
						+ "</a></li>";
			}
		}
	}
	ta += "<li><a href='javascript:pageChange(" + nextPage
			+ ");'>&raquo;</a></li>";
	$("#pagebody").html(ta);
}
// 实现全选的js代码
function sellectAll() {
	var input = document.getElementsByTagName("input");
	var notSelectCount = 0;
	var inputCount = input.length;
	for (var i = 0; i < inputCount; i++) {
		if (input[i].type == "checkbox") {
			if (input[i].checked == false) {
				notSelectCount++;
			}
		}
	}
	if (notSelectCount == 1) {
		for (var i = 0; i < inputCount; i++) {
			if (input[i].type == "checkbox") {
				input[i].checked = false;
			}
		}
	} else {
		for (var i = 0; i < inputCount; i++) {
			if (input[i].type == "checkbox") {
				input[i].checked = true;
			}
		}
	}
}

/**
 * 获取多选框的id组合
 * 
 * @returns {String}
 */
function valueGetFromCheckbox() {
	var input = document.getElementsByTagName("input");
	var inputCount = input.length;
	var values = "";
	for (var i = 0; i < inputCount; i++) {
		if (input[i].type == "checkbox" && input[i].checked
				&& input[i].className == 'arch') {
			if (values == "" && input[i].id != "") {
				values += input[i].id;
			} else if (values != "" && input[i].id != "") {
				values += "," + input[i].id;
			}
		}
	}
	return values;
}

function archDelete() {
	var values = valueGetFromCheckbox();
	if (values == "") {
		alert("请选择档案。");
		return;
	}
	$("#archivesIdStr").val(values);
	if (confirm("确认删除？")) {
		$.post("deleteArchives", $("#deleteForm").serialize(), function(responseResult) {
			alert(responseResult.responseParam.message);
			location.reload();
		});
	}
}